import cCalendar from "components/calendar";
import yzAddressList from "@/components/ui_components/yz_addressList/index";
export default {
  data() {
    return {
      location_detail: {},
      popup_show: false,
      showCalendar: false,
      calendarEvents: [],
      // options: {
        // header_font_color: '#4978F8',
        // selected_bg_color: '#4978F8',
        // header_bg_color: "#ffffff",
        // week_font_color: "#333333",
        // today_bg_color: "#bac7e9",
        // event_dot_color: "#083ce4",
        // normal_bg_color: "#ffffff",
        // normal_font_color: "#333333",
        // disabled_bg_color: "#ffffff",
        // disabled_font_color: "#cccccc",
        // pre_month_color: "#4978F8",
        // next_month_color: "#4978F8"
      // },
      time_op: '',
      timeList:[],
      time_day:"",
      save_time: "", //保存原始时间
      showAddress: false,
      id: "",
      dataDetail: {},
      content: "",
      info: false,
      myLocation: {},
      activeNames: [1]
    };
  },
  activated() {
    this.myLocation = JSON.parse(localStorage.getItem("myLocation"));
    this.id = this.$route.params.id;
    this.getAddress();
  },
  methods: {
    selectedDay(e) {
      let format_time = `${e[0]}-${e[1]}-${e[2]}`;
      this.time_day = format_time;
      this.time_op = "";
      this.getTimeList(new Date(`${e[0]}-${this.filterDate(e[1])}-${e[2]}`).getTime() / 1000);
    },
    monthChanged(e) {  //移动月份
      // console.log(e);
    },
    chooseTime(item) {
      if(item.state == 0) {
        this.time_op = item.time_str;
      }
    },
    cancalTime() {
      this.popup_show = false;
      if(this.save_time) {
        this.time_op = this.save_time[3];
        this.time_day = this.save_time[0]+'-'+this.save_time[1]+'-'+this.save_time[2]
      }else {
        this.time_op = "";
      }
    },
    clickAppointment() {
      this.save_time = this.time_op !== '' ? [this.time_day.split('-')[0],this.time_day.split('-')[1],this.time_day.split('-')[2],this.time_op] : '';
      this.time_day =  new Date().getFullYear()+"-" + (new Date().getMonth()+1) + "-" + new Date().getDate();
      this.time_op = "";
      this.getTimeGroup(new Date(this.time_day).getTime() / 1000);
    },
    confirmSelectAddress(item) {
      this.location_detail = item;
    },
    // 获取收货地址
    getAddress() {
      $http.get("member.member-address.index", {}).then((response) => {
          if (response.result === 1) {
            if(response.data.length > 0) {
              for(let item of response.data) {
                if(item.isdefault == 1) {
                  this.location_detail = item;
                }
              }
            }
            this.getData();
          } else {
            this.$toast(response.msg);
          }
        },
        function(response) {
          console.log(response);
        }
      );
    },
    // 获取详情
    getData() {
      $http.get("plugin.be-within-call.frontend.create-need.pre-ability", { aid : this.id }).then((response) => {
          if (response.result === 1) {
            this.dataDetail = response.data;
            this.info = true;
          } else {
            this.$toast(response.msg);
            this.$router.go(-1);
          }
        },
        function(response) {
          console.log(response);
        }
      );
    },
    // 选择预约指定时间
    getTimeList(date) {
      this.timeList = [];
      $http.get("plugin.be-within-call.frontend.work-time.pre-time", { aid : this.id ,date} , 'loading').then((response) => {
          if (response.result === 1) {
            this.timeList = response.data.time_list;
          } else {
            this.$toast(response.msg);
          }
          this.popup_show = true;
          this.showCalendar = true;
        },
        function(response) {
          console.log(response);
        }
      );
    },
    // 技能工作时间分组
    getTimeGroup(date) {
      $http.get("plugin.be-within-call.frontend.work-time.group", { aid : this.id}).then((response) => {
          if (response.result === 1) {
            for(let item of response.data) {
              this.calendarEvents.push(this.timestampToTime(item.date));
            }
          } else {
            this.$toast(response.msg);
          }
          this.getTimeList(date);
        },
        function(response) {
          console.log(response);
        }
      );
    },
    submit() {
      let time = new Date(`${this.time_day.split('-')[0]}/${this.time_day.split('-')[1]}/${this.time_day.split('-')[2]} ${this.time_op}`);
      let form = {
        content: this.content,
        longitude: this.myLocation ? this.myLocation.point.lng : "",
        latitude: this.myLocation ? this.myLocation.point.lat : "",
        province: this.location_detail.province,
        city: this.location_detail.city,
        district: this.location_detail.district,
        street: this.location_detail.street,
        address: this.location_detail.address,
        start_at: time.getTime() / 1000,
        finish_at: time.getTime() / 1000,
        realname: this.location_detail.username,
        mobile: this.location_detail.mobile,
        ability_id : this.id
      }
      console.log(form,'formform');
      $http.post("plugin.be-within-call.frontend.create-need.appointment", { ...form }).then((response) => {
          if (response.result === 1) {
            this.$router.push(
              this.fun.getUrl("orderpay", {
                status: 2,
                order_ids: response.data.order_ids
              })
            );
          } else {
            this.$toast(response.msg);
          }
        },
        function(response) {
          console.log(response);
        }
      );
    },
    //将时间戳转换成正常时间格式
    timestampToTime(timestamp) {
      if (!Number(timestamp) || timestamp == "") {
        return "";
      }
      let date = new Date(timestamp * 1000); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
      let Y = date.getFullYear() + "-";
      let M = (date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1) + "-";
      let D = (date.getDate() < 10 ? "0" + date.getDate() : date.getDate());
      return Y + M + D;
    },
    // 将十位数0变为空
    filterDate(value) {
      let newNum = value.split('');
      let text = ''
      if(newNum.indexOf('0') == -1) {
        text = value
      }else {
        text = newNum[1]
      }
      console.log(value,text,newNum);
      return text
    }
  },
  components: { cCalendar , yzAddressList}
};
